package com.jone.bang.util;


import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.LinkedList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;





public class RssLoadUtil {
	public static final String TAG = "RssLoadUtil";
	public static final String tbodyBegin = "<tbody>";
	public static final String tbodyEnd = "</tbody>";
	
	private static String getString(InputStream is) {
        InputStreamReader r = null;
        try {
            StringBuilder sb = new StringBuilder();
            //这里是固定把网页内容的编码写在GBK,应该是可设置的
            r = new InputStreamReader(is, "GBK");
            char[] buffer = new char[51200];
            int length = -1;
            while ((length = r.read(buffer)) != -1) {
                sb.append(new String(buffer, 0, length));
            }
            return sb.toString();
        } catch (Exception ex) {           
            return "";
        } finally {
            try {
                r.close();
            } catch (Exception ex) {
            	return "";
            }
        }
    }
	
	public static String loadUrl(String urlDestination){
		
		String ret = null ;
		try {
			URL url = new URL(urlDestination);
			HttpURLConnection conn = (HttpURLConnection)url.openConnection();   
			conn.addRequestProperty("content-type","text/html;charset=GBK");
			conn.setRequestMethod("GET");  		
			InputStream in = conn.getInputStream();		 
			ret = getString(in);
			
			in.close();		
			conn.disconnect();
			conn = null;
			
			ret = htmlTrim(ret);
			Matcher m = Pattern.compile("(?<=<tbody>).*?(?=</tbody>)",Pattern.DOTALL).matcher(ret);
			String content = null;
			if (m.find()) {
				content = m.group();				
			}
			
			StringBuffer sb = new StringBuffer();
			int totalRow = 0;
			if(content != null){
				Matcher cm = Pattern.compile("(?<=<td class=\"key\">).*?(?=</td>)",Pattern.DOTALL).matcher(content);
				
				while(cm.find()){	
					totalRow++;
					if(totalRow>20){
						break;
					}
					sb.append(cm.group()).append("\n");
				}
//				sb.insert(0, tbodyBegin);
//				sb.append(tbodyEnd);
				String total = sb.toString().replaceAll("&", "");
				return total;
			}
		} catch (Exception e) {
			//Log.e(TAG, "load data error:"+e.toString());
		}  
		return null;
	}
	
	public static String htmlTrim(String str) {
		if(str != null){
			str = str.replaceAll("\n", "");
	        str = str.replaceAll("\t", "");    
	        str = str.replaceAll("<a/?[^>]+>", "");    
	        str = str.replaceAll("</a[\\s]?>", "");    
		}
		return str;
	}
	
	public static void main(String args[]){
		
		String content = RssLoadUtil.loadUrl("http://top.baidu.com/rss/top10.xml");
		System.out.println(content);
	}

}
